package com.llc.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.llc.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2021000119618046";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDC5wSwvJr75iJrMfR3JOm02R106Ivp3NKUl1IAUa3AsTBCs9tvQYn7KBxsf2EhztC2e+m7fB9VxkyT5Mv8VxFuX4M6J0poYJy6Bgqp98FX0TJwNtslxG5TP2XlCOOudoi9Xrg/OhPMXFPyxme2pR4XoqDWgEpkj2BN1NKgo2xuau1LQHZq/ffAVTZ+KR3id7wQX3ESf0SZJTSltM39WT7c7eAA/IofNt+eYNnBUT44r/zxhiPwRtRFu+/i4ixTsiuPyqFiXj3SKVdcQG8cNBBbzgxGP3ZNvkKfDFM3m52KIn6HDYN7c9Zi+KRLS2YsoGCNW/IRMnsh3IKwKESGOrWHAgMBAAECggEATlGEyfxQDw8QH6EoSkQ0AW8JbjQQHP6WRA12bxhjSL4q1XLup4tfheQ9X7/j6TDxH5M1fhhHgzE3DmrvKUxXfdG1qsWlGcJQ2FObL2eWctJmgT8Z2thPOPaN3HYw/6c0FVydjxKoaANl3HWrsXIyYG8CUI+m1TfkDAKMTO6+oLN0TNWZDABmlUKxfVsp0bSg91gPfRrAFP45o3CeZV6LapCjE5oLBBun4kI6WE7XRo7q408GHLXmxECrA5Ja5IDx6zLCqHt8ygS7UZG0K5M/LaG5EP/UhJesylFAuAcZaNNmQ9qm8DjfKb7LY+ask/Ed+rYlpWlMhr63zs8E2ERfWQKBgQD6XU/xmeRYf9R9xVGVoabP0uThxzHsCSPkcHZ/m2I3etPUD9BEh7mg+y83M7NMYmtHxhtO9yCSjuJfOmmuOJ1DOLKEpMC4HtdX4gB5FgL0Oe90saaeqk0lcbHem3d4AIa9C7e+DRFYrQVRxb8aGm1dbfV38OlHa2DdthnpwGxJewKBgQDHSh03eLGfRlKtE280rxD1JQ9Al5Rp5bsVV2jsmagWZCb/laFJPFHwKVTgicaBHAiPZFgCFy1bqGj8FNrsBXvAKY4MM/w/9TGdRifCLg2fC34xXiuzYpUE7uzfFIvL32Wya/AcrXIyQEeDhxfRDomWW2z3lqD2gnRPO7jzOQuoZQKBgQDfUEv2XCXpKlwGsvHKXZ3E0X+GQ36r4ps5L3cC6z337n7zMAe8mLNie6npuxu6ji7SnknNbXCISRpxhlCtLaJYS0IwPiQIFrpNkTCK0K9h2CtWEv9jltOpCnh07obpas7D4KfLxSwrxGhyP8ftKJVUOLgye5gYNBb6q2sBrLjp8QKBgG23twggMaIfxEq3RWU68NoWMiR8j2AR84LXYdPI8MqLDxFtXqOESxQunSUVlSzlsyzGsNFN01IAEPSfwrtodW4hMqwzPrAznMIaqq70Tdl/KVWio+qZ3rMdm1lh5Wkzr7igRkxzjUhvLTATgyA8obEe2oog6rNstP4eQj/d6zYlAoGBAK6X1rqg17Mr+bqE5BylIsqmgLeAw75s6Tyv7mZhwGWDm3s3gzMADGXcyhpJajX+Z3u8M7+CKVEdcq4zwgaFnCqe0+KfN1+3wkq+I8EItLHiInTkqGKah97dYN3Lp1Fxzthj2h6IqgdzjkM1flm0uVLLJ1QXhrMrynQea9bn0ReI";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkjiMVMEnQxZFTP5mYb9QrsSR/JlPzAnjlyOUVW2P8Qc83S1Y8ZWwSENTZgVJoOXVP33yEL0sHPai2vMjdRl1kFFAmUM5F0B9PGCi/6pUkiVp1+TI6KqGQHRvvvnXEO9/HSbHmb8WPC8LB8XdSt3n8CgJ5B8dsKi+1MQZhtuEorkLGwcdHg7KrMhJwm3dcBsybXK43V7W0koVLZ0x/qqFwjX68TU3vjni0TcUzQhRQyR8B4m69zbTTfyH787LpQ/s9z0ue2x9aZr3SgxiWrdDmYzAVkisVFnUIRIh9sGqiIppbPEtZHxMGVbEbpl6U1j810uX66YHhEi2bCB70mlFFwIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
    //成功以后跳转的页面
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
